package o;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.framework.coreservice.DataHolder;
import com.huawei.appmarket.framework.coreservice.Status;
import com.huawei.appmarket.service.appdetail.bean.detail.DetailHiddenBean;
import com.huawei.appmarket.service.apppermission.bean.DownloadPermissionResponse.AppPermissionInfo;
import com.huawei.appmarket.service.apppermission.bean.SingleAppPermissionInfo;
import com.huawei.appmarket.service.distribution.batchdownload.bean.AgdBatchVerificationRequest;
import com.huawei.appmarket.service.distribution.batchdownload.bean.AgdBatchVerificationResponse;
import com.huawei.appmarket.service.distribution.batchdownload.bean.AppDetailInfo;
import com.huawei.appmarket.service.distribution.net.view.DistributionNetActivityProtocol;
import com.huawei.appmarket.service.distribution.net.view.DistributionNetActivityProtocol.Request;
import com.huawei.appmarket.service.distribution.permission.DistributionPermissionActivityProtocol;
import com.huawei.appmarket.service.externalapi.secureactivity.SecureProxyActivity;
import com.huawei.appmarket.service.externalservice.distribution.download.request.BatchDownloadIPCRequest;
import com.huawei.appmarket.service.externalservice.distribution.download.request.DownloadInfo;
import com.huawei.appmarket.service.externalservice.distribution.download.response.BatchDownloadResponse;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import o.dts.b;

public class dxc implements IServerCallBack {
    private long ʻ = 0;
    private List<AppPermissionInfo> ʼ = new ArrayList();
    private List<SessionDownloadTask> ʽ = new ArrayList();
    private Context ˊ;
    private DataHolder<BatchDownloadIPCRequest> ˋ;
    private dgz ˎ;
    private BatchDownloadResponse ˏ;
    private List<DownloadInfo> ॱ = new ArrayList();
    private List<b> ॱॱ = new ArrayList();
    private List<SessionDownloadTask> ᐝ = new ArrayList();

    public dxc(@NonNull Context context, @NonNull BatchDownloadResponse batchDownloadResponse, @NonNull dgz o_dgz) {
        this.ˊ = context;
        this.ˏ = batchDownloadResponse;
        this.ˎ = o_dgz;
    }

    public void ˋ(DataHolder<BatchDownloadIPCRequest> dataHolder) {
        this.ˋ = dataHolder;
        this.ॱ = ((BatchDownloadIPCRequest) this.ˋ.ˊ()).ˏ();
        ˎ();
        if (CollectCheck.isEmpty(this.ॱ) || this.ॱ.size() > 100) {
            Logger.e("AgdBatchDownloadManager", "batch download app is null or over max batch number!");
            ˎ(9, null);
            return;
        }
        cvt agdBatchVerificationRequest = new AgdBatchVerificationRequest(ˋ(), this.ˊ);
        agdBatchVerificationRequest.ॱˊ(AgdBatchVerificationRequest.ॱ(this.ॱ));
        agdBatchVerificationRequest.ʻ(1);
        agdBatchVerificationRequest.ᐝ(((BatchDownloadIPCRequest) dataHolder.ˊ()).ˋ());
        cfw.ˎ(agdBatchVerificationRequest, this);
    }

    private void ˎ() {
        Iterator it = this.ॱ.iterator();
        while (it.hasNext()) {
            if (TextUtils.isEmpty(((DownloadInfo) it.next()).ˎ())) {
                Logger.e("AgdBatchDownloadManager", "the download package is null!");
                it.remove();
            }
        }
    }

    private String ˋ() {
        return this.ˋ.ˏ().ˋ();
    }

    private String ˊ() {
        return this.ˋ.ˏ().ˏ();
    }

    private int ॱ() {
        return this.ˋ.ˏ().ˊ();
    }

    public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
    }

    public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
        if (responseBean == null) {
            Logger.ˎ("AgdBatchDownloadManager", "call store response error!");
            ˎ(13, null);
        } else if ((responseBean instanceof AgdBatchVerificationResponse) && responseBean.getResponseCode() == 0) {
            AgdBatchVerificationResponse agdBatchVerificationResponse = (AgdBatchVerificationResponse) responseBean;
            if (agdBatchVerificationResponse.getRtnCode_() != 0) {
                Logger.ˎ("AgdBatchDownloadManager", "call store error rtnCode :" + agdBatchVerificationResponse.getRtnCode_());
                this.ˏ.ॱ(agdBatchVerificationResponse.getRtnCode_());
                ˎ(8, null);
            } else if (agdBatchVerificationResponse.ˊ() == 0) {
                Logger.ˎ("AgdBatchDownloadManager", "security verification fail: " + ˋ());
                ˎ(9, null);
            } else {
                ˊ(agdBatchVerificationResponse);
            }
        } else if (responseBean.getResponseCode() == 3) {
            Logger.ˎ("AgdBatchDownloadManager", "call store error responseCode :" + responseBean.getResponseCode());
            ˎ(7, null);
        } else {
            Logger.ˎ("AgdBatchDownloadManager", "call store error responseCode :" + responseBean.getResponseCode());
            ˎ(13, null);
        }
    }

    private void ˊ(AgdBatchVerificationResponse agdBatchVerificationResponse) {
        if (ejj.ˊ().ˎ()) {
            Logger.ˎ("AgdBatchDownloadManager", "can not download because child model: " + ˋ());
            for (DownloadInfo ˎ : this.ॱ) {
                this.ˏ.ॱ(ˎ.ˎ(), 6);
            }
            ˎ(0, null);
            return;
        }
        ॱ(agdBatchVerificationResponse.ˏ());
        if (CollectCheck.isEmpty(this.ᐝ) && CollectCheck.isEmpty(this.ʽ)) {
            ˎ(0, null);
            return;
        }
        ArrayList ˊ = dts.ˊ(this.ʼ, this.ॱॱ);
        if (!CollectCheck.isEmpty(ˊ)) {
            ˎ(6, ॱ(ˊ));
        } else if (dxi.ॱ(this.ˊ, this.ˋ.ˏ().ˊ(), ((BatchDownloadIPCRequest) this.ˋ.ˊ()).ˊ())) {
            ˎ(6, ˏ());
        } else {
            for (SessionDownloadTask sessionDownloadTask : this.ᐝ) {
                sessionDownloadTask.ˋॱ(8);
                dvt.ˏ().ˊ(sessionDownloadTask.ʾ());
            }
            for (SessionDownloadTask sessionDownloadTask2 : this.ʽ) {
                dvt.ˏ().ॱ(sessionDownloadTask2, false);
            }
            ˎ(0, null);
        }
    }

    private PendingIntent ˏ() {
        cjo distributionNetActivityProtocol = new DistributionNetActivityProtocol();
        Request request = new Request();
        request.ˋ(this.ʻ);
        request.ॱ(ʻ());
        request.ˎ(ᐝ());
        distributionNetActivityProtocol.ˊ(request);
        return SecureProxyActivity.ˎ(this.ˊ, this.ˋ.ˏ().ˋ(), new cjm("distribution.net.activity", distributionNetActivityProtocol));
    }

    private PendingIntent ॱ(ArrayList<SingleAppPermissionInfo> arrayList) {
        cjo distributionPermissionActivityProtocol = new DistributionPermissionActivityProtocol();
        DistributionPermissionActivityProtocol.Request request = new DistributionPermissionActivityProtocol.Request();
        request.ˋ(this.ʻ);
        request.ˏ(ॱ());
        request.ॱ(((BatchDownloadIPCRequest) this.ˋ.ˊ()).ˊ());
        request.ॱ(ʻ());
        request.ˎ(ᐝ());
        distributionPermissionActivityProtocol.ˎ(request);
        cjm o_cjm = new cjm("distribution.permission.activity", distributionPermissionActivityProtocol);
        Intent ॱ = o_cjm.ॱ();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("permission_info_list_key", arrayList);
        ॱ.putExtra("distribution_permission_bundle_key", bundle);
        return SecureProxyActivity.ˎ(this.ˊ, this.ˋ.ˏ().ˋ(), o_cjm);
    }

    private List<String> ʻ() {
        List<String> arrayList = new ArrayList();
        for (SessionDownloadTask ˊˊ : this.ᐝ) {
            arrayList.add(ˊˊ.ˊˊ());
        }
        return arrayList;
    }

    private List<String> ᐝ() {
        List<String> arrayList = new ArrayList();
        for (SessionDownloadTask ˊ : this.ʽ) {
            arrayList.add(dph.ˋ().ˊ(ˊ));
        }
        return arrayList;
    }

    private AppDetailInfo ॱ(List<AppDetailInfo> list, @NonNull String str) {
        if (CollectCheck.isEmpty(list)) {
            return null;
        }
        for (AppDetailInfo appDetailInfo : list) {
            if (str.equals(appDetailInfo.ˋ())) {
                return appDetailInfo;
            }
        }
        return null;
    }

    private void ॱ(List<AppDetailInfo> list) {
        for (DownloadInfo downloadInfo : this.ॱ) {
            String ˎ = downloadInfo.ˎ();
            AppDetailInfo ॱ = ॱ(list, ˎ);
            if (ॱ == null) {
                Logger.ˎ("AgdBatchDownloadManager", "package:" + ˎ + " is not on shelf, so can not download!");
                this.ˏ.ॱ(ˎ, 1);
            } else if (dxj.ˋ(this.ˊ, ˎ, ॱ.ॱ())) {
                Logger.ˎ("AgdBatchDownloadManager", "package:" + ˎ + " has higher version locally, so can not download!");
                this.ˏ.ॱ(ˎ, 2);
            } else if (ॱ.ˏ() != 0) {
                Logger.ˎ("AgdBatchDownloadManager", "package:" + ˎ + " is limited access download in hiapp market!");
                this.ˏ.ॱ(ˎ, 3);
            } else {
                SessionDownloadTask ˋ = dvt.ˏ().ˋ(ˎ);
                if (ˋ != null) {
                    ॱ(downloadInfo, ˎ, ॱ, ˋ);
                } else {
                    ॱ(downloadInfo, ˎ, ॱ);
                }
            }
        }
    }

    private void ॱ(DownloadInfo downloadInfo, String str, AppDetailInfo appDetailInfo) {
        DetailHiddenBean ॱ = dxj.ॱ(appDetailInfo);
        new dxm(ॱ.ॱˌ()).execute(new Void[0]);
        SessionDownloadTask ॱ2 = dxq.ॱ(ॱ);
        if (ॱ2 == null) {
            Logger.ˎ("AgdBatchDownloadManager", "package:" + str + " can not get bundle info!");
            this.ˏ.ॱ(str, 4);
        } else {
            this.ˏ.ॱ(str, 0);
            ॱ2.ˋॱ(8);
            ॱ2.ॱˊ(dxj.ॱ(this.ˋ, downloadInfo));
            this.ʻ += appDetailInfo.ॱॱ();
            this.ʽ.add(ॱ2);
        }
        ˎ(appDetailInfo);
    }

    private void ॱ(DownloadInfo downloadInfo, String str, AppDetailInfo appDetailInfo, SessionDownloadTask sessionDownloadTask) {
        if (sessionDownloadTask.ˈ() == 6 || sessionDownloadTask.ˈ() == -1) {
            if (!dxj.ॱ(sessionDownloadTask, ˋ(), ˊ())) {
                dxj.ˊ(sessionDownloadTask, this.ˋ, downloadInfo);
            }
            this.ˏ.ॱ(str, 0);
            this.ʻ = ((long) ((1.0f - (((float) sessionDownloadTask.ʼॱ()) / 100.0f)) * ((float) appDetailInfo.ॱॱ()))) + this.ʻ;
            this.ᐝ.add(sessionDownloadTask);
        } else if (dxj.ॱ(sessionDownloadTask, ˋ(), ˊ())) {
            this.ˏ.ॱ(str, 0);
        } else {
            Logger.ˎ("AgdBatchDownloadManager", "package:" + str + " is downloading by other media!");
            this.ˏ.ॱ(str, 5);
        }
    }

    private void ˎ(AppDetailInfo appDetailInfo) {
        if (!((cdg) cdb.ॱ(cdg.class)).ॱॱ(appDetailInfo.ˋ())) {
            if (VERSION.SDK_INT < 23 || appDetailInfo.ˊ() < 23) {
                Collection ˎ = appDetailInfo.ˎ();
                if (!CollectCheck.isEmpty(ˎ)) {
                    b bVar = new b();
                    bVar.ˊ(appDetailInfo.ʽ());
                    bVar.ˏ(appDetailInfo.ˋ());
                    bVar.ˊ(appDetailInfo.ˊ());
                    this.ॱॱ.add(bVar);
                    this.ʼ.addAll(ˎ);
                }
            }
        }
    }

    private void ˎ(int i, PendingIntent pendingIntent) {
        Status status = new Status(i);
        status.ˏ(this.ˏ);
        if (pendingIntent != null) {
            status.ˊ(pendingIntent);
        }
        try {
            this.ˎ.ॱ(status);
        } catch (RemoteException e) {
            Logger.e("AgdBatchDownloadManager", "distribution error: RemoteException");
        }
    }
}
